﻿CREATE PROCEDURE [dbo].[yaf_attachment_list](
                @MessageID    INT  = NULL,
                @AttachmentID INT  = NULL,
                @BoardID      INT  = NULL)
AS
    BEGIN
        IF @MessageID IS NOT NULL
        SELECT *
        FROM   yaf_Attachment
        WHERE  MessageID = @MessageID
        ELSE
        IF @AttachmentID IS NOT NULL
        SELECT *
        FROM   yaf_Attachment
        WHERE  AttachmentID = @AttachmentID
        ELSE
        SELECT   a.*,
                 Posted = b.Posted,
                 ForumID = d.ForumID,
                 ForumName = d.Name,
                 TopicID = c.TopicID,
                 TopicName = c.Topic
        FROM     yaf_Attachment a,
                 yaf_Message b,
                 yaf_Topic c,
                 yaf_Forum d,
                 yaf_Category e
        WHERE    b.MessageID = a.MessageID
        AND c.TopicID = b.TopicID
        AND d.ForumID = c.ForumID
        AND e.CategoryID = d.CategoryID
        AND e.BoardID = @BoardID
        ORDER BY d.Name,
                 c.Topic,
                 b.Posted
    END


